package com.wanmait.recruitment.webController;

import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.pojo.*;
import com.wanmait.recruitment.service.*;
import com.wanmait.recruitment.util.RelativeDateFormat;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.imageio.ImageTranscoder;
import javax.servlet.http.HttpSession;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("zhuang/job")
public class JobController {
    @Resource
    private JobService jobService;
    @Resource
    private ConfigService configService;
    @Resource
    private MoneyService moneyService;
    @Resource
    private ExRequireService exRequireService;
    @Resource
    private DegreeService degreeService;
    @Resource
    private NatureService natureService;
    @Resource
    private ReleaseTimeService releaseTimeService;
    @Resource
    private IndustryService industryService;
    @Resource
    private CityService cityService;
    @Resource
    private CarouselService carouselService;
    @Resource
    private CompanyService companyService;
    @Resource
    private ResumeService resumeService;
    @Resource
    private DeliverService deliverService;
    @Resource
    private UserinfoService userinfoService;
    @Resource
    private CommentService commentService;
    @Resource
    private CollectionService collectionService;
    @Resource
    private MessageService messageService;
    @RequestMapping("jobList")
    @ModelAttribute("jobs")
    public List<Job> jobList(String sign) {
        List<Job> jobs = null;
        if (sign.equals("hot")) {
            try {
                jobs = jobService.selectJobList(Integer.parseInt(configService.getValueByKey("web_hotJob_count")),sign);
            } catch (NumberFormatException e) {
                jobs = jobService.selectJobList(Integer.parseInt(configService.getDefaultValueByKey("web_hotJob_count")),sign);
            }
        }
        if(sign.equals("new")){
            try {
                jobs = jobService.selectJobList(Integer.parseInt(configService.getValueByKey("web_newJob_count")),sign);
            } catch (NumberFormatException e) {
                jobs = jobService.selectJobList(Integer.parseInt(configService.getDefaultValueByKey("web_newJob_count")),sign);
            }
        }
        return jobs;
    }
    @RequestMapping("list")
    public void list(String hot,Model model,String type,String kd,Integer industrieId,String search)
    {
        List<Money>moneys = moneyService.findAll();
        model.addAttribute("moneys",moneys);
        List<ExRequire>exRequires = exRequireService.findAll();
        model.addAttribute("exRequires",exRequires);
        List<Degree>degrees = degreeService.findAll();
        model.addAttribute("degrees",degrees);
        List<Nature>natures = natureService.findAll();
        model.addAttribute("natures",natures);
        List<ReleaseTime>releaseTimes = releaseTimeService.findAll();
        model.addAttribute("releaseTimes",releaseTimes);
        List<Industry>industries = industryService.FindHots();
        model.addAttribute("industries",industries);
        List<City>cities = cityService.findAll();
        model.addAttribute("cities",cities);
        List<City>afcities = cityService.selectAF();
        model.addAttribute("afcities",afcities);
        List<City>GJcities = cityService.selectGJ();
        model.addAttribute("GJcities",GJcities);
        List<City>KNcities = cityService.selectKN();
        model.addAttribute("KNcities",KNcities);
        List<City>ORties = cityService.selectOR();
        model.addAttribute("ORties",ORties);
        List<City>Svcities = cityService.selectSV();
        model.addAttribute("Svcities",Svcities);
        List<City>WZcities = cityService.selectWZ();
        model.addAttribute("WZcities",WZcities);
        List<Carousel>carousels = carouselService.findAll();
        model.addAttribute("carousels",carousels);
        model.addAttribute("type",type);
        model.addAttribute("kd",kd);
        model.addAttribute("industrieId",industrieId);
        model.addAttribute("search",search);
        model.addAttribute("hot",hot);
    }

    @RequestMapping("listdata")
    public ModelAndView listdata( String companyname, String pagernum,Integer moneyId,Integer exRequireId,Integer degreeId,Integer natureId,Integer industrieId,String search,Integer citieId)
    {
        //页码
        int pageNum;
        try {
            pageNum = Integer.parseInt(pagernum);
        } catch (NumberFormatException e) {
            pageNum = 1;
        }
        //每页显示条数
        int pageSize;
        try {
            pageSize = Integer.parseInt(configService.getValueByKey("job_list_count"));
        } catch (NumberFormatException e) {
            pageSize = 8;
        }
        ModelAndView modelAndView = new ModelAndView();
        Job job = new Job();
        Money money = new Money();
        money.setId(moneyId);
        Nature nature = new Nature();
        nature.setId(natureId);
        ExRequire exRequire = new ExRequire();
        exRequire.setId(exRequireId);
        Degree degree = new Degree();
        degree.setId(degreeId);
        Company company = new Company();
        company.setCompanyName(companyname);
        Industry industry = new Industry();
        industry.setId(industrieId);
        City city = new City();
        city.setId(citieId);
        job.setName(search);
        job.setIndustry(industry);
        job.setCity(city);
        job.setCompany(company);
        job.setDegree(degree);
        job.setExRequire(exRequire);
        job.setMoney(money);
        job.setNatures(nature);

        PageInfo<Job> pageInfo = jobService.findPageJob(job, pageNum, pageSize);
        List<Job> jobs = pageInfo.getList();

        for (Job job1 :jobs)
        {
            job1.setTime(RelativeDateFormat.format(job1.getInputTime()));
        }
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.addObject("jobs", jobs);
        modelAndView.setViewName("zhuang/job/listdata");
        return modelAndView;

    }

    @GetMapping("jobDetail")
    public ModelAndView getJobDetail(int jobId, HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        jobService.updateLookCount(jobId);
        Job job = jobService.findById(jobId);
        Userinfo loginUser = (Userinfo)session.getAttribute("userinfo");
        Collection collection  = collectionService.findByJobAndUserinfo(job,loginUser);
        try {
            List<Resume> resumes = resumeService.findByUserinfoId(loginUser.getId());
            modelAndView.addObject("resumes",resumes);
        } catch (Exception e) {
            modelAndView.addObject("resumes",null);
        }
        try {
            Deliver deliver = deliverService.selectByUserinfoId(loginUser.getId(),jobId);
            modelAndView.addObject("deliver",deliver);
        } catch (Exception e) {
            modelAndView.addObject("deliver",null);
        }
        modelAndView.addObject("collection",collection);
        modelAndView.addObject("job",job);
        modelAndView.setViewName("zhuang/job/jobDetail");
        return modelAndView;
    }
    @PostMapping("jobDetail")
    public String detail(Integer resumeId,Integer companyId,Integer jobId,HttpSession session){
        Deliver deliver = new Deliver();

        Resume resume = new Resume();
        resume.setId(resumeId);
        deliver.setResume(resume);

        Job job = new Job();
        job.setId(jobId);
        deliver.setJob(job);

        Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
        deliver.setUserinfo(userinfo);
        deliverService.insert(deliver);
        jobService.updateResumeCount(jobId,1);
        companyService.updateResumeCount(companyId,1);
        userinfoService.updateNowResumeCount(userinfo.getId());
        Message message = new Message();
        message.setContent("投递");
        message.setCompanyId(companyId);
        messageService.insertUserDeliver(message);
        return "redirect:/zhuang/job/jobDetail?jobId="+jobId;
    }
    @RequestMapping("confirmCancel")
    @ResponseBody
    public void confirmCancel(Integer deliverId,Integer jobId,Integer companyId){
        deliverService.delete(deliverId);
        jobService.updateResumeCount(jobId,-1);
        companyService.updateResumeCount(companyId,-1);
    }
    @RequestMapping("collection")
    @ResponseBody
    public void collection(Integer jobId,String sign,HttpSession session){
        Collection collection = new Collection();
        Job job = new Job();
        job.setId(jobId);
        collection.setJob(job);
        Userinfo loginUser = (Userinfo) session.getAttribute("userinfo");
        collection.setUserinfo(loginUser);
        if(sign.equals("collection")){
            collectionService.insert(collection);
        }
        if(sign.equals("cancelColl")){
            collectionService.deleteByColl(collection);
        }
    }
    @RequestMapping("comment1")
    public ModelAndView comment1(Integer jobId,Integer pageNumber){
        ModelAndView modelAndView = new ModelAndView();
        Comment comment = new Comment();
        Job job = new Job();
        job.setId(jobId);
        comment.setJob(job);
        int pageSize;
        try {
            pageSize = Integer.parseInt(configService.getValueByKey("web_newJob_count"));
        } catch (NumberFormatException e) {
            pageSize = Integer.parseInt(configService.getDefaultValueByKey("web_newJob_count"));
        }
        int pageNum;
        try {
            pageNum = pageNumber;
        } catch (Exception e) {
            pageNum = 1;
        }
        PageInfo<Comment> commentPageInfo = commentService.findPage(comment,pageNum,pageSize);
        List<Comment>comments = commentPageInfo.getList();

        List<Integer> str = new ArrayList<>();
        if (comments.size()>0) {
            for (Comment comment1 : comments) {
                str.add(comment1.getUserinfo().getId());
            }
            List<Comment> comments1 = commentService.selectListNextByUserinfoIds(str);

            modelAndView.addObject("commentPageInfo", commentPageInfo);
            modelAndView.addObject("comments", comments);
            modelAndView.addObject("comments1", comments1);
            return modelAndView;
        }
        else {
            return modelAndView;
        }
    }
    @PostMapping("comment1")
    @ResponseBody
    public String comments(Comment comment,Integer jobId,Integer userinfoId){
        Job job = new Job();
        Userinfo userinfo = new Userinfo();
        if (comment.getParentId()!=0){
            job.setId(jobId);
            comment.setJob(job);
            userinfo.setId(userinfoId);
            comment.setUserinfo(userinfo);
            comment.setEnable(true);
            commentService.insert(comment);
            return "y";
        }
        else {
            comment.setParentId(0);
            job.setId(jobId);
            comment.setJob(job);

            userinfo.setId(userinfoId);
            comment.setUserinfo(userinfo);
            comment.setEnable(true);
            commentService.insert(comment);
            return "y";
        }

    }
}
